package com.xius.controller;

import cn.hutool.core.lang.Dict;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageInfo;
import com.xius.common.ApiErrorCode;
import com.xius.common.R;
import com.xius.entity.domain.JobAndTrigger;
import com.xius.entity.form.JobForm;
import com.xius.service.JobService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


/**
 * <p>
 * Job Controller
 * </p>
 *
 * @author yangkai.shen
 * @date Created in 2018-11-26 13:23
 */
@RestController
@RequestMapping("/job")
@Slf4j
public class JobController {
    private final JobService jobService;

    @Autowired
    public JobController(JobService jobService) {
        this.jobService = jobService;
    }

    /**
     * 保存定时任务
     */
    @PostMapping
    public R addJob( JobForm form) {
        try {
            jobService.addJob(form);
        } catch (Exception e) {
            return R.failed(ApiErrorCode.ERROR);
        }

        return R.ok("增加成功");
    }

    /**
     * 删除定时任务
     */
    @DeleteMapping
    public R deleteJob(JobForm form) throws SchedulerException {
        if (StrUtil.hasBlank(form.getJobGroupName(), form.getJobClassName())) {
            return R.failed("参数不能为空");
        }

        jobService.deleteJob(form);
        return R.ok("删除成功");
    }

    /**
     * 暂停定时任务
     */
    @PutMapping(params = "pause")
    public R pauseJob(JobForm form) throws SchedulerException {
        if (StrUtil.hasBlank(form.getJobGroupName(), form.getJobClassName())) {
            return  R.failed("参数不能为空");
        }

        jobService.pauseJob(form);
        return R.ok("暂停成功");
    }

    /**
     * 恢复定时任务
     */
    @PutMapping(params = "resume")
    public R resumeJob(JobForm form) throws SchedulerException {
        if (StrUtil.hasBlank(form.getJobGroupName(), form.getJobClassName())) {
            return  R.failed("参数不能为空");
        }

        jobService.resumeJob(form);
        return R.ok("恢复成功");
    }

    /**
     * 修改定时任务，定时时间
     */
    @PutMapping(params = "cron")
    public R cronJob(JobForm form) {
        try {
            jobService.cronJob(form);
        } catch (Exception e) {
            return R.failed(ApiErrorCode.ERROR);
        }
        return R.ok("修改成功");
    }

    @GetMapping
    public R jobList(Integer currentPage, Integer pageSize) {
        if (ObjectUtil.isNull(currentPage)) {
            currentPage = 1;
        }
        if (ObjectUtil.isNull(pageSize)) {
            pageSize = 10;
        }
        PageInfo<JobAndTrigger> all = jobService.list(currentPage, pageSize);
        return R.ok(Dict.create().set("total", all.getTotal()).set("data", all.getList()));
    }

}
